package com.example2.demo.controller;

import com.example2.demo.entity.TestEntity;
import com.example2.demo.service.TestService;
import com.jxzx.common.utils.PageUtils;
import com.jxzx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author stargis
 * @email stargis@gmail.com
 * @date 2019-01-24 15:38:35
 */
@Api(tags = "")
@RestController
@RequestMapping("demo/test")
public class TestController {
    @Autowired
    private TestService testService;

    /**
     * 列表分页
     */
    @ApiOperation(value = "列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String" ),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String" ),
    })
    @GetMapping("/page")
    @RequiresPermissions("demo:test:page")
    public R page(
            @RequestParam(name = "limit",required = false) String limit,
            @RequestParam(name = "page",required = false) String currPage
    ){
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(limit)){
            params.put("limit",limit);
        }
        if(StringUtils.isNotBlank(currPage)){
            params.put("page",currPage);
        }
        PageUtils page = testService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    @ApiImplicitParam(name = "params", value = "查询条件", dataType = "Map")
    @RequiresPermissions("demo:test:list")
    public R list(@RequestParam Map<String, Object> params){
        List list = testService.selectByMap(params);

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer")
    @RequiresPermissions("demo:test:info")
    public R info(@PathVariable("id") Integer id){
            TestEntity test = testService.selectById(id);

        return R.ok().put("test", test);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    @ApiImplicitParam(name = "test", value = "Test实体", dataType = "TestEntity")
    @RequiresPermissions("demo:test:save")
    public R save(@RequestBody TestEntity test){
        testService.insert(test);

        return R.ok().put("ID",test.getId());
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @ApiImplicitParam(name = "test", value = "Test实体", dataType = "TestEntity")
    @RequiresPermissions("demo:test:update")
    public R update(@RequestBody TestEntity test){
		testService.updateById(test);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "test", value = "Test实体", dataType = "TestEntity")
    @RequiresPermissions("demo:test:delete")
    public R delete(@RequestBody Integer[] ids){
		testService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
